package cn.stylefeng.guns.modular.wx;

import cn.stylefeng.guns.modular.api.tips.ApiResponseData;
import cn.stylefeng.guns.modular.course.entity.Category;
import cn.stylefeng.guns.modular.course.entity.Course;
import cn.stylefeng.guns.modular.course.service.CategoryService;
import cn.stylefeng.guns.modular.course.service.CourseService;
import cn.stylefeng.guns.modular.customer.entity.Customer;
import cn.stylefeng.guns.modular.customer.service.CustomerService;
import cn.stylefeng.guns.modular.yy.entity.Banner;
import cn.stylefeng.guns.modular.yy.service.BannerService;
import net.bytebuddy.implementation.auxiliary.AuxiliaryType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;


@RequestMapping(value = "web")
@Controller
public class IndexController {
    private String PREFIX = "/web";


    @Autowired
    CategoryService categoryService;

    @Autowired
    CourseService courseService;

    @Autowired
    BannerService bannerService;

    @Autowired
    CustomerService customerService;

    @Autowired
    HttpSession session;



    @RequestMapping("index")
    public String index(Model model) {
        //模拟一个用户
        Customer customer=customerService.getById(1);
         session.setAttribute("customer",customer);
         if(customer.getMobile()==null||!customer.getMobile().equals("")){
             model.addAttribute("mobile",0);
         }else{
             //没有手机号要弹窗绑定
             model.addAttribute("mobile",1);
         }

        //课程分类
        Category category=new Category();
        category.setPid(0);
        List<Category> categoryList=categoryService.findListBySpec(category);
         model.addAttribute("category",categoryList);
        //轮播图
        List<Banner> bannerList=bannerService.findListBySpec(new Banner());
        model.addAttribute("banner",bannerList);

        //免费课程
        Course course=new Course();
        course.setPrice(new BigDecimal(0));
        List<Course> courseList= courseService.findPageBySpec(course).getData();


        model.addAttribute("free",courseList);

        //推荐课程
        Course hot=new Course();
        hot.setIsHot(1);
        List<Course> hotCourseList= courseService.findPageBySpec(hot).getData();
        model.addAttribute("hot",hotCourseList);

        return PREFIX + "/index/index.html";
    }

    /*
    绑定手机号
     */
    @RequestMapping("bindPhone")
    public ApiResponseData index(String phone) {

        Customer customer=(Customer) session.getAttribute("customer");
        customer.setMobile(phone);

       return ApiResponseData.success("绑定成功",null);
    }




}
